package com.zm.rpc.procedure;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.zm.rpc.procedure.pojo.ProcedureCallResult;
import com.zm.rpc.procedure.pojo.ProcedureMetadata;

@Component
public class ProcedureCenter implements IProcedureCenter{
    private static final Logger logger = Logger.getLogger(ProcedureCenter.class);
    private Map<String,ProcedureCaller> procedureCallerMap;
    
    public ProcedureCenter(){
        this.procedureCallerMap = new HashMap<>();
    }

    @Override
    public void register(ProcedureMetadata metadata) {
        logger.debug("Procedure register :" + metadata.toString());
        this.procedureCallerMap.putIfAbsent(metadata.getPath(), new ProcedureCaller(metadata));
    }

    @Override
    public ProcedureCallResult call(String procedurePath, Object... params) {
        ProcedureCaller procedureCaller = this.procedureCallerMap.get(procedurePath);
        ProcedureCallResult result = null;
        if(procedureCaller != null){
            result = procedureCaller.call(params);
        }else{
            result = ProcedureCallResult.ERROR("Procedure path not found! Path:" + procedurePath, null);
        }

        return result;
    }




}
